﻿//创建时间: 2011-06-30
//创建作者: 李 城
//功能说明: 评论列表
//最后修改: 2011-07-01
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

using System.Data;
using System.Text;
using System.Collections;
using Newtonsoft.Json;
using TRPOP.CMS.Plugin;

namespace TRPOP.CMS.Web.Admin
{
    public partial class ContentClick_list : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_strPagePermissionExtraKey = "Plugin_contentclick";
        }
        #endregion

        #region 页面变量
        protected string m_title = "列表";
        protected IContentClickLog mIContentClickLog = ILogAccess.GetContentClickInstance();
        protected int mintNodeID = 0;
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
                if (mintNodeID > 0)//验证用户是否有权限操作当前结点
                    base.AlertForNoPermission(base.CheckIsHavePermission(mintNodeID));
            }
        }
        protected void btSearch_Click(object sender, EventArgs e)
        {
            if (sender is LinkButton)
            {
                txtStartTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            string strRedirectURL = "ContentClick_list.aspx?Nodeid=" + GetRequestViewState("Nodeid");
            if (!string.IsNullOrEmpty(txtKeyWords.Text))
            {
                strRedirectURL += "&KeyWords=" + txtKeyWords.TextTrim;
            }
            if ((!string.IsNullOrEmpty(txtStartTime.Text)) && (!string.IsNullOrEmpty(txtEndTime.Text)))
            {
                strRedirectURL += "&StartTime=" + txtStartTime.TextTrim + "&EndTime=" + txtEndTime.TextTrim;
            }
            if (!string.IsNullOrEmpty(txtEliteWeight.TextTrim))
            {
                strRedirectURL += "&EliteWeight=" + txtEliteWeight.TextTrim;
            }
            if (!string.IsNullOrEmpty(txtTopWeight.TextTrim))
            {
                strRedirectURL += "&TopWeight=" + txtTopWeight.TextTrim;
            }
            strRedirectURL += "&UserId=" + ddlAddUser.SelectedValue;
            strRedirectURL += "&OrderByIndex=" + ddlSort.SelectedIndex;
            int intPageSize = 0;
            if (int.TryParse(txtPageSize.TextTrim, out intPageSize) && intPageSize > 0)
            {
                strRedirectURL += "&PageSize=" + intPageSize;
            }
            Response.Redirect(strRedirectURL, true);
        }
        #endregion

        #region 页面方法
        public void LoadBase()
        {
            /*
             *       初始化
             *   nodeid                   保存在试图里
             *   标题                     绑定时根据文本框的值进行判断
             *   时间段                   绑定时根据文本框的值进行判断
             *   精华                     绑定时根据文本框的值进行判断
             *   置顶                     绑定时根据文本框的值进行判断
             *   作者                     绑定时根据文本框的值进行判断
             *   排序                     绑定时根据文本框的值进行判断
             *   单页显示内容的大小       绑定时根据文本框的值进行判断
             */
            //Nodeid 的处理
            mintNodeID = InitRequestViewState("Nodeid").ToInt();
            m_title = "信息列表";
            //标题
            if (string.IsNullOrEmpty(InitRequestViewState("KeyWords")) == false)
            {
                txtKeyWords.Text = GetRequestViewState("KeyWords");
            }
            //开始时间结束时间 的处理
            if (string.IsNullOrEmpty(InitRequestViewState("StartTime")) || string.IsNullOrEmpty(InitRequestViewState("EndTime")))
            {
                //不再给时间段增加默认值
                //txtStartTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                //txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            else
            {
                txtStartTime.Text = InitRequestViewState("StartTime");
                txtEndTime.Text = InitRequestViewState("EndTime");
            }
            //if (string.IsNullOrEmpty(txtEndTime.Text) || string.IsNullOrEmpty(InitRequestViewState("StartTime")))
            //{
            //    txtStartTime.Text = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + "-01";
            //    txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            //}
            // 精华
            if (string.IsNullOrEmpty(InitRequestViewState("EliteWeight")) == false)
            {
                txtEliteWeight.Text = GetRequestViewState("EliteWeight");
            }
            //置顶
            if (string.IsNullOrEmpty(InitRequestViewState("TopWeight")) == false)
            {
                txtTopWeight.Text = GetRequestViewState("TopWeight");
            }
            //作者
            ddlAddUser.Items.Clear();
            ddlAddUser.Items.Add(new ListItem("全部用户", "0"));
            foreach (TRPOP.CMS.UserManage.AdminUserInfo oTempInfo in IAdminUserAccess.GetInstance().GetUserListByCondition(0, "", ""))
            {
                ddlAddUser.Items.Add(new ListItem(oTempInfo.Name, oTempInfo.UserBaseId.ToString()));
            }
            if (string.IsNullOrEmpty(InitRequestViewState("UserId")) == false)
            {
                ddlAddUser.SelectedValue = GetRequestViewState("UserId");
            }
            //排序
            if (string.IsNullOrEmpty(InitRequestViewState("OrderByIndex")) == false)
            {
                int intSelectIndex = GetRequestViewState("OrderByIndex").ToInt();
                if (intSelectIndex >= 0 && intSelectIndex <= 2)
                {
                    ddlSort.SelectedIndex = intSelectIndex;
                }
            }
            // 单页显示内容的大小
            if (string.IsNullOrEmpty(InitRequestViewState("PageSize")) == false)
            {
                int intPageSize = GetRequestViewState("PageSize").ToInt();
                if (intPageSize > 0)
                {
                    txtPageSize.Text = intPageSize.ToString();
                }
            }
            if (string.IsNullOrEmpty(InitRequestViewState("page")) == false)
            {
                if (GetRequestViewState("page").ToInt() <= 0)
                    ViewState["page"] = 1;
            }
            else
            {
                ViewState["page"] = 1;
            }
            /*
             * 
             *    绑定数据 
             *      根据各个 文本框 droplist 的值来绑定
             * 
             * 
             */
            //条件
            string strCondation = string.Empty;
            //*****结点
            string NodeCondation = string.Empty;
            if (mintNodeID > 0)
            {
                foreach (NodeInfo oNodeInfo in new NodeManage().GetNodeListByParentID(mintNodeID))
                {
                    NodeCondation += oNodeInfo.NodeId + ",";
                }
                if (string.IsNullOrEmpty(NodeCondation) == false)
                {
                    NodeCondation += mintNodeID;
                    strCondation += " AND [NodeId] in ( " + NodeCondation + " ) ";
                    m_title += "  -> " + (new NodeManage()).GetInfoById(mintNodeID).Name;
                }
            }
            else
            {
                m_title += "  ->  全部结点";
            }
            //*****关键字条件
            if (txtKeyWords.TextTrim.Length > 0)
            {
                bool blnIsHaveCondation = false;
                foreach (string strTemp in txtKeyWords.TextTrim.SQLFilter().Split(' '))
                {
                    if (string.IsNullOrEmpty(strTemp) == false)
                    {
                        if (blnIsHaveCondation == false)
                        {
                            strCondation += " AND ( ";
                            strCondation += "       [ContentName] like '%" + strTemp + "%' ";
                            blnIsHaveCondation = true;
                        }
                        else
                        {
                            strCondation += "       OR [ContentName]  like '%" + strTemp + "%' ";
                        }
                    }
                }
                if (blnIsHaveCondation)
                {
                    strCondation += "             )   ";
                }
            }
            //*****时间段条件
            if (string.IsNullOrEmpty(txtStartTime.Text) == false && string.IsNullOrEmpty(txtStartTime.Text) == false)
            {
                DateTime oStart;
                if (DateTime.TryParse(txtStartTime.TextTrim, out oStart) == false)
                {
                    oStart = DateTime.Now;
                }
                DateTime oEnd;
                if (DateTime.TryParse(txtEndTime.TextTrim, out oEnd) == false)
                {
                    oEnd = DateTime.Now;
                }
                strCondation += string.Format(" AND [PubTime] >= '{0}-{1}-{2} 00:00:00' AND  [PubTime] <= '{3}-{4}-{5} 23:59:59' ",
                                            oStart.Year, oStart.Month, oStart.Day, oEnd.Year, oEnd.Month, oEnd.Day);

            }
            //******精华
            if (string.IsNullOrEmpty(txtEliteWeight.Text) == false)
            {
                int intEliteWeight = 0;
                int.TryParse(txtEliteWeight.Text, out intEliteWeight);
                if (intEliteWeight > 0)
                {
                    strCondation += " AND [EliteWeight] = " + intEliteWeight + "  ";
                }
            }
            //******置顶
            if (string.IsNullOrEmpty(txtTopWeight.Text) == false)
            {
                int intTopWeight = 0;
                int.TryParse(txtTopWeight.Text, out intTopWeight);
                if (intTopWeight > 0)
                {
                    strCondation += " AND [TopWeight] = " + intTopWeight + "  ";
                }
            }
            //******作者
            if (ddlAddUser.SelectedValue.ToInt() > 0)
            {
                strCondation += " AND  [AddUserId] = " + ddlAddUser.SelectedValue;
            }
            //绑定Repeater
            int intRecord;
            using (DataSet oDataset = mIContentClickLog.GetListByPager(GetRequestViewState("page").ToInt(),
                                             txtPageSize.Text.ToInt(),
                                             strCondation,
                                             ddlSort.SelectedValue,//排列顺序
                                             out intRecord))
            {
                if (oDataset != null && oDataset.Tables.Count > 0 && oDataset.Tables[0].Rows.Count > 0)
                {
                    rptList.DataSource = oDataset.Tables[0];
                    rptList.DataBind();
                }
                else
                    return;
            }
            Pager.PageSize = txtPageSize.Text.ToInt();
            Pager.ShowRecordCount = true;
            Pager.ShowPageSize = true;
            Pager.ShowCurrentPage = true;
            Pager.RecordCount = intRecord;
        }
        #endregion

    }
}
